package zhupf.test.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier与CountDownLatch正好相反，CountDownLatch是做线程数量减法，到零的时候启动等待线程
 * CyclicBarrier是做加法，等到调用CyclicBarrier是做加法.await()方法的线程数量到达指定的数量时，启动设置好的线程
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("条件符合，启动功能线程");
        });
        for(int i = 1;i<=7;i++){
            final int temp = i;
            new Thread(()->{
                System.out.println("运行第:"+temp+"个前提线程");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
